Objective-C Runtime bindings and wrapper for Rust.
- Documentation: http://ssheldon.github.io/rust-objc/objc/
- Crate: https://crates.io/crates/objc
Messaging objects
Objective-C objects can be messaged using the msg_send!
macro:
let cls = class!;
let obj: *mut Object = msg_send!;
let hash: usize = msg_send!;
let is_kind: BOOL = msg_send!;
// Even void methods must have their return type annotated
let _: = msg_send!;
Reference counting
The utilities of the rc
module provide ARC-like semantics for working with
Objective-C's reference counted objects in Rust.
A StrongPtr
retains an object and releases the object when dropped.
A WeakPtr
will not retain the object, but can be upgraded to a StrongPtr
and safely fails if the object has been deallocated.
// StrongPtr will release the object when dropped
let obj = unsafe ;
// Cloning retains the object an additional time
let cloned = obj.clone;
autoreleasepool;
// Weak references won't retain the object
let weak = obj.weak;
drop;
assert!;
Declaring classes
Classes can be declared using the ClassDecl
struct. Instance variables and
methods can then be added before the class is ultimately registered.
The following example demonstrates declaring a class named MyNumber
that has
one ivar, a u32
named _number
and a number
method that returns it:
let superclass = class!;
let mut decl = new.unwrap;
// Add an instance variable
decl.;
// Add an ObjC method for getting the number
extern
unsafe
decl.register;
Exceptions
By default, if the msg_send!
macro causes an exception to be thrown, this
will unwind into Rust resulting in unsafe, undefined behavior.
However, this crate has an "exception"
feature which, when enabled, wraps
each msg_send!
in a @try
/@catch
and panics if an exception is caught,
preventing Objective-C from unwinding into Rust.
Message type verification
The Objective-C runtime includes encodings for each method that describe the argument and return types. This crate can take advantage of these encodings to verify that the types used in Rust match the types encoded for the method.
To use this functionality, enable the "verify_message"
feature.
With this feature enabled, type checking is performed for every message send,
which also requires that all arguments and return values for all messages
implement Encode
.
If this requirement is burdensome or you'd rather just verify specific messages,
you can call the Message::verify_message
method for specific selectors.
Support for other Operating Systems
The bindings can be used on Linux or *BSD utilizing the GNUstep Objective-C runtime.